﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Credisoft.Models.Helpers;
using Credisoft.Models.Credisoft_BO;
using Credisoft.Models.Credisoft_DAO;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Credisoft.Models;

namespace Credisoft.Controllers.InitialForm
{
    public class CreditGrantingController : Controller
    {
        //
        // GET: /CreditGranting/

        public ActionResult CreditGranting()
        {
            return View();
        }

        /* ======== EJEMPLO DE LLAMADA ======== */
        //public ActionResult startInbox()
        //{
        //    String jsonString = @"{ ""esExtraordinario"" : true }"; // Falta testear lo de las variables
        //    JObject variables = JObject.Parse(jsonString);
        //    String message = BonitaConnector.startProcess(
        //        BonitaConnector.PROCESS_ONE_INBOX_ASSIGNMENT,
        //        BonitaConnector.ID_INSTANTIATE_PROCESS_WITH_VARIABLES,
        //        variables);

        //    ViewBag.Message = message;
        //    return View();
        //}

        [HttpPost]
        public JsonResult insertCreditGrantingS1(CreditGrantingStationBO creditGranting)
        {
            CreditGrantingDAO creditDAO = new CreditGrantingDAO();

            // BUILD AND REGISTER 'OTORGAMIENTO'
            otorgamiento solicitud = new otorgamiento();
            solicitud.extraordinario = false; // <------------------ REVISAR
            solicitud.fecharegistro = DateTime.Now;
            solicitud.monto = Decimal.Parse(creditGranting.cash + "");
            solicitud = creditDAO.insertCreditGranting(solicitud);

            // BUILD AND REGISTER 'OTORGAMIENTOESTACION'
            otorgamientoestacion solicitudEstacion = new otorgamientoestacion();
            solicitudEstacion.idestacion = 1 ;
            solicitudEstacion.otorgamiento = solicitud;
            solicitudEstacion.idotorgamiento = solicitud.idotorgamiento;
            creditDAO.insertCreditGrantingByStation(solicitudEstacion);

            // BUID AND REGISTER 'ESTADOFINANCIERO'
            estadosfinancieros eeff = new estadosfinancieros();
            //eeff.activocorriente;

            // GET VARIABLES FOR BONITA
            int idJefeGrupo = WorkgroupDAO.getWorkGroupChiefByEconomicSectorId(creditGranting.economicSector);
            int idSolicitudEmpresa = 1;
            int idEstacion = 1;
            bool esExtraordinario = false;

            // BUILD JOBJECT TO START PROCESS
            JObject variables = new JObject(
                new JProperty("idSolicitudEmpresa", idSolicitudEmpresa + "" ),
                new JProperty("idJefeGrupo", idJefeGrupo + "" ),
                new JProperty("idEstacion", idEstacion + "" ),
                new JProperty("esExtraordinario", esExtraordinario )
                );

            // CALL BONITA PROCESS
            String bonitaId = BonitaConnector.startProcess(
                BonitaConnector.PROCESS_ONE_INBOX_ASSIGNMENT,
                BonitaConnector.ID_INSTANTIATE_PROCESS_WITH_VARIABLES,
                variables
            );

            solicitud.id_proceso_bonita = bonitaId;
            creditDAO.modifyCreditGranting( solicitud ) ;

            return new JsonResult() { Data =  creditGranting };
        }
    }
}
